<?php

     include('common.php');

    if (session_is_registered(MySBB_Admin_username) and $member_permission == 1)
    {

// **  **

         if ($groupper_row['admincp_subject'] != 1)
         {
             $SF->error('المعذره .. هذه الميزه غير متاحه لك !');
         }

// **  **

         if ($_GET['trash'] == 1)
         {
             if ($_GET['subject'] == 1)
             {
                 if ($_GET['index'] == 1)
                 {
                     $getdelsubject_query = $DB->sql_query("SELECT * FROM " . $db_prefix . "subject WHERE delete_topic='1' ORDER BY id DESC");

                     $getdelsubject_rows = array();
                     while ($getdelsubject_row = $DB->sql_fetch_array($getdelsubject_query))
                     {
                         $getdelsubject_rows[] = $getdelsubject_row;
                         $Smarty->assign('getdelsubject_rows',$getdelsubject_rows);
                     }

                     $SF->Make_cplink_path('المواضيع المحذوفه');
                     $Smarty->display('subject_trash_main.tpl');
                 }

                 if ($_GET['re'] == 1)
                 {
                     $update = $DB->sql_query("UPDATE " . $db_prefix . "subject SET delete_topic='0' WHERE id='" . intval($_GET['id']) . "'");
                     $update_member = $DB->sql_query("UPDATE " . $db_prefix . "member SET posts=posts+1 WHERE username='" . $_GET['writer'] . "'");
                     $update_reply  = $DB->sql_query("UPDATE " . $db_prefix . "reply SET delete_topic='0' WHERE subject_id='" . intval($_GET['id']) . "'");

                     if ($update)
                     {
                         $SF->msg('تم الاسترجاع بنجاح !');
                         $SF->go_to('subject.php?trash=1&subject=1&index=1',2);
                     }
                 }
             }

             if ($_GET['reply'] == 1)
             {
                 if ($_GET['index'] == 1)
                 {
                     $getdelreply_query = $DB->sql_query("SELECT * FROM " . $db_prefix . "reply WHERE delete_topic='1' ORDER BY id DESC");

                     $getdelreply_rows = array();
                     while ($getdelreply_row = $DB->sql_fetch_array($getdelreply_query))
                     {
                         $getdelreply_rows[] = $getdelreply_row;
                         $Smarty->assign('getdelreply_rows',$getdelreply_rows);
                     }

                     $SF->Make_cplink_path('الردود المحذوفه');
                     $Smarty->display('reply_trash_main.tpl');
                 }

                 if ($_GET['re'] == 1)
                 {
                     $update = $DB->sql_query("UPDATE " . $db_prefix . "reply SET delete_topic='0' WHERE id='" . intval($_GET['id']) . "'");
                     $update_member = $DB->sql_query("UPDATE " . $db_prefix . "member SET posts=posts+1 WHERE username='" . $_GET['writer'] . "'");

                     if ($update)
                     {
                         $SF->msg('تم الاسترجاع بنجاح !');
                         $SF->go_to('subject.php?trash=1&reply=1&index=1',2);
                     }
                 }

                 if ($_GET['del_reply'] == 1)
                 {

                     $getinfo_query = $DB->sql_query("SELECT * FROM " . $db_prefix . "attach WHERE subject_id='" . intval($_GET['id']) . "'");
                     $getinfo_num   = $DB->sql_num_rows($getinfo_query);

                     if ($getinfo_num > 0)
                     {
                         $getinfo_row   = $DB->sql_fetch_array($getinfo_query);
                         $delete_attach = unlink('../' . $info_row['download_path'] . '/' . $getinfo_row['filename']);
                         $delete_row    = $DB->sql_query("DELETE FROM " . $db_prefix . "attach WHERE id='" . $getinfo_row['id'] . "'");
                     }

                     $del = $DB->sql_query("DELETE FROM " . $db_prefix . "reply WHERE id='" . intval($_GET['id']) . "'");
                     if ($del)
                     {
                         $updateforums       =         $SF->UpdateForumsArray();
                         $updategroup        =         $SF->UpdateSectionGroupArray();
                         $updatesubforums         =         $SF->UpdateSubForums();
                         $SF->msg('تم الحذف بنجاح !');
                         $SF->go_to('subject.php?trash=1&reply=1&index=1',2);
                     }
                 }
             }
         }

// **  **

         if ($_GET['close'] == 1)
         {
             if ($_GET['index'] == 1)
             {
                 $getsubject_query = $DB->sql_query("SELECT * FROM " . $db_prefix . "subject WHERE close='1' ORDER BY id DESC");

                 $getsubject_rows = array();
                 while ($getsubject_row = $DB->sql_fetch_array($getsubject_query))
                 {
                     $getsubject_rows[] = $getsubject_row;
                     $Smarty->assign('getsubject_rows',$getsubject_rows);
                 }

                 $SF->Make_cplink_path('المواضيع المغلقه');
                 $Smarty->display('subject_main.tpl');
             }
         }

// **  **

         if ($_GET['attach'] == 1)
         {
             if ($_GET['index'] == 1)
             {
                 $getattachsubject_query = $DB->sql_query("SELECT * FROM " . $db_prefix . "attach");
                 while ($getattachsubject_row = $DB->sql_fetch_array($getattachsubject_query))
                 {
                     $getsubject_query = $DB->sql_query("SELECT * FROM " . $db_prefix . "subject WHERE id='" . $getattachsubject_row['subject_id'] . "'");

                     $getsubject_rows = array();
                     while ($getsubject_row = $DB->sql_fetch_array($getsubject_query))
                     {
                         $getsubject_rows[] = $getsubject_row;
                         $Smarty->assign('getsubject_rows',$getsubject_rows);
                     }
                 }

                 $SF->Make_cplink_path('المواضيع التي بها مرفقات');
                 $Smarty->display('subject_main.tpl');
             }
         }

// **  **

         if ($_GET['mass'] == 1)
         {
             if ($_GET['del_subject'] == 1)
             {
                 if ($_GET['index'] == 1)
                 {
                     $SF->Make_cplink_path('حذف جماعي للمواضيع');
                     $getsection_query = $DB->sql_query("SELECT * FROM " . $db_prefix . "section WHERE main_section<>'1' AND linksection<>'1'");

                     $getsection_rows = array();
                     while ($getsection_row = $DB->sql_fetch_array($getsection_query))
                     {
                         $getsection_rows[] = $getsection_row;
                         $Smarty->assign('getsection_rows',$getsection_rows);
                     }

                     $Smarty->display('subject_mass_del.tpl');
                 }

                 if ($_GET['start_del'] == 1)
                 {
                     if ($_POST['section'] == 0)
                     {
                         $SF->error('يرجى اختيار القسم !');
                     }

                     $update = $DB->sql_query("UPDATE " . $db_prefix . "subject SET delete_topic='1' WHERE section='" . $_POST['section'] . "'");
                     $update = $DB->sql_query("UPDATE " . $db_prefix . "reply SET delete_topic='1' WHERE section='" . $_POST['section'] . "'");

                     if ($update)
                     {
                         $SF->msg('تم الحذف بنجاح !');
                         $SF->go_to('index.php?nomenu=1',2);
                     }
                 }
             }

             if ($_GET['move_subject'] == 1)
             {
                 if ($_GET['index'] == 1)
                 {
                     $SF->Make_cplink_path('نقل جماعي للمواضيع');
                     $getsection_query = $DB->sql_query("SELECT * FROM " . $db_prefix . "section WHERE main_section<>'1'AND linksection<>'1'");

                     $getsection_rows = array();
                     while ($getsection_row = $DB->sql_fetch_array($getsection_query))
                     {
                         $getsection_rows[] = $getsection_row;
                         $Smarty->assign('getsection_rows',$getsection_rows);
                     }

                     $Smarty->display('subject_move_del.tpl');
                 }

                 if ($_GET['start_move'] == 1)
                 {
                     if ($_POST['from_section'] == 0 or $_POST['to_section'] == 0)
                     {
                         $SF->error('يرجى اختيار القسم !');
                     }

                     if ($_POST['from_section'] == $_POST['to_section'])
                     {
                         $SF->error('يرجى اختيار قسم المراد النقل إليه .. حيث لا يمكن نقل المواضيع إلى نفس القسم');
                     }

                     $update = $DB->sql_query("UPDATE " . $db_prefix . "subject SET section='" . $_POST['to_section'] . "' WHERE section='" . $_POST['from_section'] . "'");
                     $update = $DB->sql_query("UPDATE " . $db_prefix . "reply SET section='" . $_POST['to_section'] . "' WHERE section='" . $_POST['from_section'] . "'");

                     if ($update)
                     {
                         $SF->msg('تم النقل بنجاح !');
                         $SF->go_to('index.php?nomenu=1',2);
                     }
                 }
             }
         }

// **  **

         if ($_GET['del'] == 1)
         {
             $getinfo_query = $DB->sql_query("SELECT * FROM " . $db_prefix . "attach WHERE subject_id='" . intval($_GET['id']) . "'");
             $getinfo_num   = $DB->sql_num_rows($getinfo_query);

             if ($getinfo_num > 0)
             {
                 $getinfo_row   = $DB->sql_fetch_array($getinfo_query);
                 $delete_attach = unlink('../' . $info_row['download_path'] . '/' . $getinfo_row['filename']);
                 $delete_row    = $DB->sql_query("DELETE FROM " . $db_prefix . "attach WHERE id='" . $getinfo_row['id'] . "'");
             }

             $del = $DB->sql_query("DELETE FROM " . $db_prefix . "subject WHERE id='" . intval($_GET['id']) . "'");

             if ($del)
             {
                 $SF->msg('تم الحذف بنجاح !');
                 $SF->go_to('subject.php?trash=1&subject=1&index=1',2);
             }
         }

// **  **

    }

?>